<template>
 <div>
    <h1>角色列表</h1>
    <RouterLink :to="{path:'/role/add'}">
     <el-button  >
          add
        </el-button>
      </RouterLink>
    <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="编号" width="180" />
    <el-table-column prop="role" label="角色名称" width="180" />
    <el-table-column  label="角色权限" >
       <template #default="scope">
      <el-tree
    style="max-width: 600px"
    :data="scope.row.tree"
    :props="defaultProps"
    @node-click="handleNodeClick"
  />
  </template>
    </el-table-column>
    <el-table-column label="Operations">
      <template #default="scope">
        <el-button size="small" @click="Edit(scope.row.id)">
          Edit
        </el-button>
         <el-popconfirm title="Are you sure to delete this?" @confirm="Delete(scope.row)">
          <template #reference>
        <el-button
          size="small"
          type="danger"
        >
          Delete
        </el-button>
        </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  </div>
</template>

<script setup lang="ts">
import {getrole,getroledel} from '../../request/index'
import {ref,reactive} from 'vue'
import toTree from '@/utils/toTree';

const tableData=ref([])
getrole().then(res=>{
  console.log('role',res)
  let resdata=res.data.data
  resdata=resdata.map(item=>{
    item.tree=toTree(item.tree)
    return item
  })
console.log(resdata)
  tableData.value=resdata
})
const defaultProps = {
  children: 'children',
  label: 'name',
}

import {useRouter} from 'vue-router'
let router=useRouter()

const Edit=(id)=>{
  router.push('/role/edit/'+id)
}

const Delete=(row)=>{
  console.log(row.id)
getroledel(row.id).then(res=>{
  console.log('del',res)
  if(res.data.code==200){
    tableData.value.forEach((item,index)=>{
      if(item.id==row.id){
        tableData.value.splice(index,1)
      }
    })
  }
  
})
}

const handleNodeClick=()=>{

}

// const tableData = [
//   {
//     date: '2016-05-03',
//     name: 'Tom',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-02',
//     name: 'Tom',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-04',
//     name: 'Tom',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
//   {
//     date: '2016-05-01',
//     name: 'Tom',
//     address: 'No. 189, Grove St, Los Angeles',
//   },
// ]
</script>

<style>

</style>